package net.thxjj.adminServer.service.impl;

import net.thxjj.adminServer.dao.MobileUserDao;
import net.thxjj.adminServer.service.IMobileUserService;
import net.thxjj.sitermodel.MobileUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class MobileUserService implements IMobileUserService {

    @Autowired
    private MobileUserDao mobileUserDao;

    @Override
    public List<MobileUser> getUserList(String pid, String username) {
        return mobileUserDao.getUserList(pid,username);
    }

    @Override
    public Long getUserCounts(String pid) {
        return mobileUserDao.getUserCount(pid);
    }

    @Override
    public Long getUserCountsByTime(String pid) {
        Calendar now = Calendar.getInstance();
        long end_time_long = now.getTime().getTime();
        now.add(Calendar.HOUR, -24);
        long start_time_long = now.getTime().getTime();

        return mobileUserDao.getUserCountByTime(start_time_long/1000,end_time_long/1000,pid);
    }

    @Override
    public List<List<Object>> UserTrendsOfLastMonth(String pid) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        long endTime = calendar.getTime().getTime();

        calendar.add(Calendar.DAY_OF_MONTH,-30);

        long startTime = calendar.getTime().getTime();


        long ads = startTime;

        List<List<Object>> dea = new ArrayList<>();
        long count_init = mobileUserDao.getUserCountBeforeTime(startTime,pid);
        while (ads<=endTime) {

            long end_buf = ads+3600*24*1000;
            long list1 = mobileUserDao.getUserCountByTime(ads,end_buf,pid);
            count_init = count_init+list1;

            List<Object> listaa = new ArrayList<>();
            listaa.add(ads);
            listaa.add(count_init);
            dea.add(listaa);
            ads+=3600*24*1000;
        }

        return dea;
    }
}
